﻿using Framework.EncryptionDecryptionService.Enums;
using Framework.EncryptionDecryptionService.Inteface;
using Microsoft.Extensions.DependencyInjection;

namespace Framework.EncryptionDecryptionService.Achieve
{
    /// <summary>
    /// 加解密服务-使用
    /// </summary>
    public sealed class EncryptionDecryptionService : IEncryptionDecryptionService
    {
        /// <summary>
        /// 服务
        /// </summary>
        private readonly IServiceProvider serviceProvider;

        /// <summary>
        /// 加解密服务-使用
        /// </summary>
        /// <param name="provider"></param>
        public EncryptionDecryptionService(IServiceProvider provider) 
        {
            serviceProvider = provider;
        }

        public IDecryptionService DecryptionService(EncryptionAlgorithm decryptionEnum)
        {
            try
            {
                return serviceProvider.GetRequiredKeyedService<IDecryptionService>(decryptionEnum);
            }
            catch (Exception ex)
            {
                throw new Exception($"解密服务 {decryptionEnum} 未注入", ex);
            }
        }

        public IEncryptionService EncryptionService(EncryptionAlgorithm decryptionEnum)
        {
            try
            {
                return serviceProvider.GetRequiredKeyedService<IEncryptionService>(decryptionEnum);
            }
            catch (Exception ex)
            {
                throw new Exception($"加密服务 {decryptionEnum} 未注入", ex);
            }
        }
    }
}
